<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/14
 * Time: 17:16
 */
class CountWeightService
{
    private $type;
    private $type_id;
    private $module = 'count_weight';
    private $topicModel;
    private $diaryListCacheModel;

    function __construct($type = '', $type_id = 0)
    {
        $this->type                 =   AC::$count_weight_type[$type];
        $this->type_id              =   $type_id;
        $this->topicModel          	=   new TopicModel();
        $this->diaryListCacheModel  =   new DiaryListCacheModel();
    }

    /**
     * 初始化加权数据
     * @param $param
     * @return bool|int|resource
     */
    public function initCountWeight($param)
    {
        if (intval($this->type_id) < 1 || empty($this->type) || empty($param)) return false;

        return $this->topicModel->initCountWeight($this->type, $this->type_id, $param);
    }

    /**
     * 修改加权数据
     * @param $param
     * @return bool|int|resource
     */
    public function editCountWeight($param)
    {
        if (intval($this->type_id) < 1 || empty($this->type) || empty($param)) return false;

        $data = $this->getCountWeight();
        if (empty($data)) {
            return fasle;
        }

        $param = $this->updateWeightCacheData($data, $param);
        $result = $this->topicModel->editCountWeight($this->type, $this->type_id, $param);
        if (empty($result)) {
            return fasle;
        }

        $this->delCountWeightCache();

        return $this->getCountWeight();
    }

    /**
     * 获取记录的加权数据
     * @return bool|void
     */
    public function getCountWeight()
    {
        if (intval($this->type_id) < 1 || empty($this->type)) return false;

        $data = $this->getCountWeightCache();
        if (empty($data))
        {
            $data = $this->getCountWeightOfMysql();
            if ($data)
            {
                $this->setCountWeightCache($data);
            }
        }

        if (empty($data)) return array();
        return $this->restructCountWeightInfoData($data);
    }

    /**
     * 获取记录的加权数据
     * @return bool|void
     */
    public function getCountWeightOfMysql()
    {
        if (intval($this->type_id) < 1 || empty($this->type)) return false;

        return $this->topicModel->getCountWeight($this->type, $this->type_id);
    }

    /**
     * 查看加权计数列表
     * @param $id
     * @return array|bool
     */
    public function getCountWeightList($id)
    {
        if (empty($this->type)) return false;

        return $this->topicModel->getCountWeightList($this->type, $id);
    }

    /**
     * 获取加权计数缓存数据
     * @return mixed
     */
    private function getCountWeightCache()
    {
        $key      = $this->type.'-'.$this->type_id;
        $hash_key = CacheConf::getCacheKeyDetail($this->module, 'hash', $key);

        return $this->diaryListCacheModel->getHashCache($hash_key);
    }

    /**
     * 设置/更新加权计数缓存数据
     * @param $param
     */
    private function setCountWeightCache($param)
    {
        $key      = $this->type.'-'.$this->type_id;
        $hash_key = CacheConf::getCacheKeyDetail($this->module, 'hash', $key);
        $sset_key = CacheConf::getCacheKeyDetail($this->module, 'sset', $key);
        $expire   = CacheConf::getCacheKeyDetail($this->module, 'expire', $key);
        $data     = $this->restructCountWeightCacheData($param);

        return $this->diaryListCacheModel->setHashCache($hash_key, $sset_key, $expire, $data, $key);
    }

    /**
     * 删除加权计数缓存数据
     * @return mixed
     */
    private function delCountWeightCache()
    {
        $key      = $this->type.'-'.$this->type_id;
        $hash_key = CacheConf::getCacheKeyDetail($this->module, 'hash', $key);
        $sset_key = CacheConf::getCacheKeyDetail($this->module, 'sset', $key);

        return $this->diaryListCacheModel->detHashCache($hash_key, $sset_key);
    }

    /**
     * 组装缓存数据
     * @param $param
     * @return array
     */
    private function restructCountWeightCacheData($param)
    {
        return array(
            'n1'    =>  intval($param['n1']),
            'n2'    =>  intval($param['n2']),
            'n3'    =>  intval($param['n3']),
            'n4'    =>  intval($param['n4']),
            'n5'    =>  intval($param['n5']),
            'n6'    =>  intval($param['n6']),
            'n7'    =>  intval($param['n7']),
            'n8'    =>  intval($param['n8']),
            'n9'    =>  intval($param['n9'])
        );
    }

    private function updateWeightCacheData($data, $param)
    {
        $res = array();
        isset($param['n1']) && $res['n1'] = intval($param['n1'])  + intval($data['n1']);
        isset($param['n2']) && $res['n2'] = intval($param['n2'])  + intval($data['n2']);
        isset($param['n3']) && $res['n3'] = intval($param['n3'])  + intval($data['n3']);
        isset($param['n4']) && $res['n4'] = intval($param['n4'])  + intval($data['n4']);
        isset($param['n5']) && $res['n5'] = intval($param['n5'])  + intval($data['n5']);
        isset($param['n6']) && $res['n6'] = intval($param['n6'])  + intval($data['n6']);
        isset($param['n7']) && $res['n7'] = intval($param['n7'])  + intval($data['n7']);
        isset($param['n8']) && $res['n8'] = intval($param['n8'])  + intval($data['n8']);
        isset($param['n9']) && $res['n9'] = intval($param['n9'])  + intval($data['n9']);

        return $res;
    }

    private function restructCountWeightInfoData($param)
    {
        return array(
            'type'      =>  $this->type,
            'type_id'   =>  $this->type_id,
            'n1'        =>  intval($param['n1']),
            'n2'        =>  intval($param['n2']),
            'n3'        =>  intval($param['n3']),
            'n4'        =>  intval($param['n4']),
            'n5'        =>  intval($param['n5']),
            'n6'        =>  intval($param['n6']),
            'n7'        =>  intval($param['n7']),
            'n8'        =>  intval($param['n8']),
            'n9'        =>  intval($param['n9'])
        );
    }
}